<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a :class="{active: sorter === 'weight_desc'}" @click="getList('weight_desc')"  href="javascript:;">推荐</a>
      <a  :class="{active: sorter === null}" @click="getList(null)" href="javascript:;">最新</a>
      <div class="logo"><img
          src="@/assets/logo.png"
          alt=""
        ></div>
    </nav>

    <!-- 使用封装的组件 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in articleList"
        :key="item.id"
        :item="item"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import { interviewListApi } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      articleList: [],
      current: 1, // 当前页
      pageSize: 10, // 每页请求的数据
      sorter: 'weight_desc',
      list: [],
      loading: false,
      finished: false
    }
  },
  created () {
  },
  methods: {
    async onLoad () {
      const res = await interviewListApi({
        current: this.current, // 当前页
        pageSize: this.pageSize, // 每页请求的数据
        // weight_desc 推荐，最新-不需要传
        sorter: this.sorter
      })
      this.current++
      // 保存列表数据
      this.articleList.push(...res.data.rows)
      // 数据请求结束后，结束这一次的加载功能
      this.loading = false
      // pageTotal 表示数据的总页数
      // 判断到当当前页码大于数据的总页数的时候 结束请求
      if (this.current > res.data.pageTotal) {
        this.finished = true
      }
      console.log('滚动到底部了', this.loading)
    },
    getList (sorter) {
      console.log(sorter)
      this.sorter = sorter
      // 重置数据
      this.current = 1
      this.finished = false
      this.articleList = []
      // 调用onLoad 实现复用
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: "";
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
